import sys
import subprocess
import _thread
import os

from PySide2 import QtWidgets
from PySide2.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QGridLayout, QPushButton, QMessageBox, QTextBrowser

from tool import util, storage

downloadPathKey = "downloadPath"

class DownloadVideo(QWidget):
    def __init__(self):
        super().__init__()

        self.defaultConfig()

        self.setUI()

    def defaultConfig(self):
        path = storage.getValue(downloadPathKey)
        if not path:
            path = os.path.expanduser('~')
        self.download_path = path


    def setUI(self):

        self.edit = QLineEdit("")
        self.button = QPushButton("下载")
        self.button.clicked.connect(self.onClickDownload)

        self.infoButton = QPushButton("查看信息")
        self.infoButton.clicked.connect(self.onClickInfo)
        
        self.editFile = QLineEdit(self.download_path)
        self.buttonFile = QPushButton("选择文件夹")
        self.buttonFile.clicked.connect(self.onClickFile)

        self.textBrowser = QTextBrowser()
        
        cleanButton = QPushButton("清除")
        cleanButton.clicked.connect(self.onClickClean)
        
        grid = QGridLayout()
        self.setLayout(grid)

        grid.addWidget(self.edit, 0, 0)
        grid.addWidget(self.button, 0, 1)
        grid.addWidget(self.infoButton, 0, 2)

        grid.addWidget(self.editFile, 1, 0)
        grid.addWidget(self.buttonFile, 1, 1)
        grid.addWidget(self.textBrowser, 2, 0)
        grid.addWidget(cleanButton, 2, 1)

    def onClickInfo(self):
        text = self.edit.text()
        if len(text) == 0:
            # TODO: 提示
            return
        command = "you-get --json " + text
        reply = util.terminal(command)
        self.textBrowser.append(reply)

    def onDownload(self, threadName, delay):
        # 下载
        self.button.setEnabled(False)
        self.button.setText("下载中")
        text = self.edit.text()
        file = self.editFile.text()

        # 腾讯视频处理
        reply = util.get_download(text, file)

        self.textBrowser.append(reply)

        self.textBrowser.ensureCursorVisible()  # 游标可用
        cursor = self.textBrowser.textCursor()  # 设置游标
        pos = len(self.textBrowser.toPlainText())  # 获取文本尾部的位置
        cursor.setPosition(pos)  # 游标位置设置为尾部
        self.textBrowser.setTextCursor(cursor)  # 滚动到游标位置

        self.button.setEnabled(True)
        self.button.setText("下载")

        _thread.exit()

        self.textBrowser.append("完成下载")


    def onClickDownload(self):
        text = self.edit.text()
        if len(text) == 0 :
            # TODO: 提示
            return
        self.textBrowser.append("开始下载")

        # 开线程
        _thread.start_new_thread(self.onDownload, ("Thread-download-1", 0))


    def onClickClean(self):
        self.textBrowser.clear()
    
    def onClickFile(self):
        m = QtWidgets.QFileDialog.getExistingDirectory(
            None, "选取文件夹", self.download_path)  # 起始路径
        if len(m) > 0 :
            self.download_path  = m
            self.editFile.setText(m)
            storage.store(downloadPathKey, m)

# https://v.qq.com/x/page/w3244kjwc48.html

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = DownloadVideo()
    desktop = QApplication.desktop()
    view.resize(desktop.width()*0.5, desktop.height()*0.5)
    view.move(100, 0)
    view.setWindowTitle('下载视频工具')
    view.show()
    sys.exit(app.exec_())
